//
//  MoneyStatisticsViewController.m
//  carcareIOS
//
//  Created by ileo on 15/7/8.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "MoneyStatisticsViewController.h"
#import "PieChartLayer.h"
#import "NetMoney.h"

@interface MoneyStatisticsViewController ()

@property (nonatomic, strong) UILabel *drivingL;
@property (nonatomic, strong) UILabel *mileL;
@property (nonatomic, strong) UILabel *parkingL;
@property (nonatomic, strong) UILabel *shareL;

@property (nonatomic, strong) PieChartLayer *pieLayer;

@end

@implementation MoneyStatisticsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [CommentHelper useFunction:Use_PointStatistics];
    
    self.title = @"挣点统计";
    
    
    self.scrollView.backgroundColor = [Theme contentColor];
    
    self.pieLayer = [PieChartLayer layer];
    self.pieLayer.frame = CGRectMake(0, 0, __MAIN_WIDTH, 350 * __MAIN_RATIO_375);
    self.pieLayer.maxRadius = 100;
    self.pieLayer.minRadius = 65;
    self.pieLayer.startAngle = -30;
    [self.scrollView.layer addSublayer:self.pieLayer];
    
    UILabel *unitL = [Factory labelWithFrame:CGRectMake(25, 315 * __MAIN_RATIO_375 + 5, __MAIN_WIDTH - 50, 20) font:FONT_DEFAULT_Light(14) text:@"单位：挣点" textColor:[Theme wordGrayColor] onView:self.scrollView textAlignment:NSTextAlignmentCenter];
    
    CGFloat height = (__MAIN_WIDTH == 320) ? 40 : 48;
    self.drivingL = [self createViewWithColor:[Theme yellowColor] title:@"驾驶奖励" frame:CGRectMake(0, unitL.bottom + height - 20, __MAIN_WIDTH, height)];
    self.parkingL = [self createViewWithColor:[Theme blueColor] title:@"停驶奖励" frame:CGRectMake(0, unitL.bottom + 2 * height - 20, __MAIN_WIDTH, height)];
    self.mileL = [self createViewWithColor:[Theme mainColor] title:@"里程奖励" frame:CGRectMake(0, unitL.bottom + 3 * height - 20, __MAIN_WIDTH, height)];
    self.shareL = [self createViewWithColor:[Theme orangeColor] title:@"邀请奖励" frame:CGRectMake(0, unitL.bottom + 4 * height - 20, __MAIN_WIDTH, height)];
    
    self.scrollView.bounces = NO;
    self.scrollView.height = __MAIN_HEIGHT_64;
    self.scrollView.contentSize = CGSizeMake(__MAIN_WIDTH, unitL.bottom + 5 * height + (unitL.bottom + 5 * height > __MAIN_HEIGHT_64 ? height : 0));

    
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    __weak __typeof(self) wself = self;
    [[NetMoney moneyStatistics] requestSuccess:^(id JSON) {
        
        NSInteger driving = [JSON[@"rewardDriving"] integerValue];
        NSInteger mile = [JSON[@"rewardMiles"] integerValue];
        NSInteger parking = [JSON[@"rewardParking"] integerValue];
        NSInteger share = [JSON[@"rewardShare"] integerValue];
        NSInteger sum = driving + mile + parking + share;
        
        wself.drivingL.text = [NSString stringWithFormat:@"%zd",driving];
        wself.mileL.text = [NSString stringWithFormat:@"%zd",mile];
        wself.parkingL.text = [NSString stringWithFormat:@"%zd",parking];
        wself.shareL.text = [NSString stringWithFormat:@"%zd",share];
        
        if (sum == 0) {
            
            [Factory labelWithFrame:wself.pieLayer.frame font:FONT_DEFAULT_Light(28) text:@"暂无奖励" textColor:[Theme wordGrayColor] onView:wself.view textAlignment:NSTextAlignmentCenter];
            
        }else{
            
            NSInteger dri = (NSInteger)(driving*100.0/sum+0.5);
            NSInteger mil = (NSInteger)(mile*100.0/sum+0.5);
            NSInteger par = (NSInteger)(parking*100.0/sum+0.5);
            NSInteger sha = 100 - dri - mil - par;
            
            PieModel *drivingModel = [[PieModel alloc] init];
            drivingModel.value = driving;
            drivingModel.title = [NSString stringWithFormat:@"%zd%%",dri];
            drivingModel.color = [Theme yellowColor];
            
            PieModel *mileModel = [[PieModel alloc] init];
            mileModel.value = mile;
            mileModel.title = [NSString stringWithFormat:@"%zd%%",mil];
            mileModel.color = [Theme mainColor];
            
            PieModel *parkingModel = [[PieModel alloc] init];
            parkingModel.value = parking;
            parkingModel.title = [NSString stringWithFormat:@"%zd%%",par];
            parkingModel.color = [Theme blueColor];
            
            PieModel *shakeModel = [[PieModel alloc] init];
            shakeModel.value = share;
            shakeModel.title = [NSString stringWithFormat:@"%zd%%",sha];
            shakeModel.color = [Theme orangeColor];
            
            [wself.pieLayer updateValueModels:@[drivingModel,parkingModel,mileModel,shakeModel]];
            
        }
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UILabel *)createViewWithColor:(UIColor *)color title:(NSString *)title frame:(CGRect)frame{
    UIView *view = [[UIView viewWithFrame:frame] setupOnView:self.scrollView];
    [Factory labelWithFrame:CGRectMake(50*__MAIN_RATIO_375, 0, 100, CGRectGetHeight(frame)) font:FONT_DEFAULT_Light(15) text:title textColor:[Theme wordColor] onView:view textAlignment:NSTextAlignmentLeft];
    UILabel *numL = [Factory labelWithFrame:CGRectMake(CGRectGetWidth(frame) - 150*__MAIN_RATIO_375, 0, 100, CGRectGetHeight(frame)) font:FONT_DEFAULT_Light(15) text:nil textColor:[Theme wordColor] onView:view textAlignment:NSTextAlignmentRight];
    UIView *circle = [[[UIView viewWithFrame:CGRectMake(30, CGRectGetHeight(frame)/2-4, 8, 8)] resetBackgroundColor:color] setupOnView:view];
    circle.layer.cornerRadius = 4;
    [[[UIView viewWithFrame:CGRectMake(27*__MAIN_RATIO_375, CGRectGetHeight(frame)-1, CGRectGetWidth(frame) - 55*__MAIN_RATIO_375, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:view];
    return numL;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
